/**
 * Created by Allen on 2017/9/1.
 */




String.prototype.endWith = function(str){
	 if(str==null || str=="" || this.length == 0 ||str.length > this.length){	
       return false;
	 }
	 if(this.substring(this.length - str.length)){
		 return true;
	 }else{
		 return false;
	 }
	 return true;
};
var admin = "admin";
var sales = "sales";
var alipayManager = "alipay";
var access = "access";
var CommonUtils = {
    productSrchLifeIds:function(){
        var userObj = JSON.parse(sessionStorage.loginUser);
        if (this.checkNull(userObj) || this.checkNull(userObj.roles)){
            return [];
        }
        if(this.checkRole("admin")){
            return [];
        }
        if(this.checkRole(access)){
            return [];
        }
        let userLifes = userObj.aliLifes;
        let resultIds = [];
        if (this.checkNull(userObj.aliLifes)){
            return ["无数据"];
        }
        for(arv=0;arv<userObj.aliLifes.length;arv++){
            var aliObj = userObj.aliLifes[arv];
            resultIds.push(aliObj.id);
        }
        return resultIds;

    },

    loadIds:function(arr){
        var resultArr = [];
        if(this.checkNotNull(arr)){
            for(av1=0;av1<arr.length;av1++){
                var aobj = arr[av1];
                resultArr.push(aobj.id);
            }
        }
        return resultArr;
    },
    alilifeSrchLifeIds:function(lifes){
        let resultIds = [];
        if (this.checkNull(lifes)){
            return ["无数据"];
        }
        for(arv=0;arv<lifes.length;arv++){
            var aliObj = lifes[arv];
            resultIds.push(aliObj.id);
        }
        return resultIds;

    },
    checkRole:function(role){
        var userObj = JSON.parse(sessionStorage.loginUser);
        if (this.checkNull(userObj) || this.checkNull(userObj.roles)){
            return false;
        }
        for(crv=0;crv<userObj.roles.length;crv++){
            var roleObj = userObj.roles[crv];
            if(roleObj.code == role){
                return true;
            }
        }
        return false;
    },
    genVerificationCode:function(){
        // 验证码组成库
        var arrays=[
            '1','2','3','4','5','6','7','8','9','0',
            'a','b','c','d','e','f','g','h','i','j',
            'k','l','m','n','o','p','q','r','s','t',
            'u','v','w','x','y','z',
            'A','B','C','D','E','F','G','H','I','J',
            'K','L','M','N','O','P','Q','R','S','T',
            'U','V','W','X','Y','Z'
        ];
        // 重新初始化验证码
        var codes ='';
        // 随机从数组中获取四个元素组成验证码
        for(var i = 0; i<6; i++){
            // 随机获取一个数组的下标
            var r = parseInt(Math.random()*arrays.length);
            codes += arrays[r];
        }
        return codes;
    },
    checkNull:function(obj){
        if(typeof(obj) == 'undefined' || obj == null || obj.toString() == '' || obj.length<=0){
            return true;
        }
        return false;
    },
    checkNotNull:function (obj){
        if(typeof(obj) != 'undefined' && obj != null && obj.toString() != ''){
            return true;
        }
        return false;
    },
    checkMobile:function (str) {
        var re = /^1\d{10}$/;
        if (re.test(str)) {
            return true;
        } else {
            return false;
        }
    },
    checkAccount:function (str) {
        var re = /^[a-zA-z]\w{3,15}$/;
        if (re.test(str)) {
            return true;
        } else {
            return false;
        }
    },
    checkEmail:function (str) {
        var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
        if (re.test(str)) {
            return true;
        } else {
            return false;
        }
    },
    checkAppName:function (str) {
        var re = /[\u4E00-\u9FA5]{2,11}/g;
        if (re.test(str)) {
            return true;
        } else {
            return false;
        }
    },
    checkAppNameEng:function (str) {
        var re = /^[a-zA-Z0-9]{2,20}$/;
        if (re.test(str)) {
            return true;
        } else {
            return false;
        }
    },
    checkPassword:function (str) {
        var re = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/;
        if (re.test(str)) {
            return true;
        } else {
            return false;
        }
    },
    checkPermission:function(permission, code) {
        var loginUser = JSON.parse(sessionStorage.loginUser);
        if(this.checkNull(loginUser)){
            return false;
        }
        for(var i=0;i<permission.length;i++){
            if(permission[i]==code){
                return true;
            }
        }
    },
    checkGroup:function(code){
        var loginUser = JSON.parse(sessionStorage.loginUser);
        if(loginUser.group.code == code){
            return true;
        }
    },
    getPermissionJson:function(){
        let user = sessionStorage.loginUser;
        var userJson = JSON.parse(user)
        var roles = userJson['roles'];
        var roleJson = {};
        
        if (roles) {
            roles.forEach(element => {
                let code = element['code'];
                roleJson[code] = 1;
            });
        }
        return roleJson;
    }

}

function GetUrlRelativePath()
{
    var url = document.location.toString();
    var arrUrl = url.split("//");

    var start = arrUrl[1].indexOf("/");
    var relUrl = arrUrl[1].substring(start);//stop省略，截取从start开始到结尾的所有字符

    if(relUrl.indexOf("?") != -1){
        relUrl = relUrl.split("?")[0];
    }
    return relUrl;
}

var MAIN_INFO_CONFIG = {
    mainLogo:"./resource/images/main_logo.png",
    mainLogo2: "./resource/images/main_logo2.png",
    qrCodeBGImgUrl:"http://www.arkj168.cn/user_data/Bg1.jpg",
    qrCodeLogo:"http://www.arkj168.cn/aierkeji-site/cms-site/resource/images/45c73c4b7cdd4c9ad86741961b049de.png",
    appTitle:"艾尔智能科技"
}

var currentHost = window.location.hostname;
var rPath = GetUrlRelativePath();
var bizCode = '';
if(CommonUtils.checkNotNull(rPath)){
    if(CommonUtils.checkNotNull(rPath.split('/')[1])){
        bizCode = rPath.split('/')[1];
    }
}

$.ajax({
    type: "get",
    url: ctx+"/aux/getLogoConfig",
    dataType: 'json', //【jsonp进行跨域请求 只支持get】
    data:{ //【这里填写是传给服务端的数据 可传可不传 数据必须是json格式】
        "host":currentHost,
        "bizCode":bizCode
    },
    async: false,
    timeout: 5000,
    xhrFields: {withCredentials: true},
    success: function(response) { //【成功回调】
        if(response.status == '00'){
            let configArr = response.data;
            if(CommonUtils.checkNull(configArr)){
                return;
            }
            for(let ci=0;ci<configArr.length;ci++){
                let config = configArr[ci];
                if(config.type == '05'){
                    if(CommonUtils.checkNull(config.value)){
                        continue;
                    }
                }else{
                    if(CommonUtils.checkNull(config.imgFile) || CommonUtils.checkNull(config.imgFile.absPath)){
                        continue;
                    }
                }
                switch (config.type) {
                    case '01':
                        MAIN_INFO_CONFIG.mainLogo=config.imgFile.absPath;
                        break;
                    case '02':
                        MAIN_INFO_CONFIG.mainLogo2=config.imgFile.absPath;
                        break;
                    case '03':
                        MAIN_INFO_CONFIG.qrCodeBGImgUrl=config.imgFile.absPath;
                        break;
                    case '04':
                        MAIN_INFO_CONFIG.qrCodeLogo=config.imgFile.absPath;
                        break;
                    case '05':
                        MAIN_INFO_CONFIG.appTitle=config.value;
                        document.title = MAIN_INFO_CONFIG.appTitle;
                        break;

                }
            }
        }
    },
    error: function(xhr, type) { //【失败回调】
    }
});

